import { _decorator, Component, Node, director, Prefab, instantiate, randomRangeInt, find } from 'cc';
import { eventTarget, GameEvent } from './EventSystem';
const { ccclass, property } = _decorator;


@ccclass('GameManger')
export class GameManger extends Component {
    @property(Node)
    GamePause: Node = null;
    @property(Node)
    GameResume: Node = null;
    @property(Node)
    BackMenu: Node = null;
    @property(Node)
    GameRestart: Node = null;
    @property(Node)
    GameOver: Node = null;

    @property(Prefab)
    enemyPrefab: Prefab;
    @property(Node)
    enemyParent: Node = null;
    @property
    createEnemyRate: number = 1;
    

    createEnemyTimer: number = 0;
    private _currentHp: number = 0;


    gamePause() {
        if (director.isPaused() == false) {
            director.pause();
            this.GamePause.active = false;
            this.GameResume.active = true;
            this.BackMenu.active = true;
        }
    }
    gameResume() {
        if (director.isPaused() == true) {
            director.resume();
            this.GameResume.active = false;
            this.BackMenu.active = false;
            this.GamePause.active = true;
        }
    }
    backMenu() {
        //确保引擎恢复运行
        if (director.isPaused()) {
            director.resume();
        }
        //停止敌人生成计时器
        this.createEnemyTimer = 0;
        director.loadScene("Menu");
    }
    gameRestart() {
        if (director.isPaused()) {
            director.resume();
        }
        this.createEnemyTimer = 0;
        director.loadScene("Game");
    }

    protected onLoad(): void {
        this.initUI();

        //强制启动敌机生成计时器
        this.createEnemyTimer = this.createEnemyRate; //下一帧立即生成
        //监听血量变化事件
        eventTarget.on(GameEvent.PLAYER_HP_CHANGED, this.onHpChanged, this);
        eventTarget.on(GameEvent.PLAYER_DIED, this.onPlayerDied, this);
    }
    protected onDestroy(): void {
        //确保敌人生成逻辑隔离
        this.createEnemyTimer = 0;
        //清理事件监听
        eventTarget.off(GameEvent.PLAYER_HP_CHANGED, this.onHpChanged, this);
        eventTarget.off(GameEvent.PLAYER_DIED, this.onPlayerDied, this);
    }
    start() {
        this.initUI();

    }
    private initUI() {
        this.GameResume.active = false;
        this.BackMenu.active = false;
        this.GameRestart.active = false;
        this.GameOver.active = false;
    }
    private onHpChanged(hp: number) {
        this._currentHp = hp;
        this.updateGameState();
    }
    private onPlayerDied() {
        if (director.isPaused() == false) {
            director.pause();
            this.GameRestart.active = true;
            this.BackMenu.active = true;
            this.GameOver.active = true;
        }
    }
    private updateGameState(){
        //在此处理其他血量相关逻辑
        //便于后续游戏的功能扩展
    }
    update(deltaTime: number) {

        this.createEnemyTimer += deltaTime;
        if (this.createEnemyTimer >= this.createEnemyRate) {
            this.createEnemyTimer = 0;
            this.createEnemy();
        }
    }
    createEnemy() {
        if (this.enemyPrefab == null) return;
        //从敌机预制体生成新敌机节点
        const EnemyPrefab = instantiate(this.enemyPrefab);
        //把新敌机节点放进EnemyParent父节点中
        this.enemyParent.addChild(EnemyPrefab);
        //设置新敌机节点生成位置，记得同步世界坐标
        let enemyX = randomRangeInt(20, 360);
        EnemyPrefab.setWorldPosition(enemyX, this.enemyParent.worldPosition.y, this.enemyParent.worldPosition.z);
    }

}


